﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using MuaDeal.DTO;


namespace MuaDeal
{
    public partial class XemDSGHTheoTenKH : Form
    {
        public XemDSGHTheoTenKH()
        {
            InitializeComponent();
        }

        private void XemDSGHTheoTenKH_Load(object sender, EventArgs e)
        {
            //Load combobox mã khách hàng
             try
             {
                Datahelper helper = new Datahelper();
                helper.Open();
                string proc = "sp_LayDanhSachUser";
                  DataTable dt = new DataTable();
                dt = helper.ExecuteQueryStore(proc);
                 helper.Close();
                 cmtenkh.DataSource = dt;
                 cmtenkh.DisplayMember = dt.Columns[0].ToString();
                 

                 //Load lên listview

                 string procgh = "sp_LayDSGioHang";
                 DataTable dtghang = new DataTable();
                 dtghang = helper.ExecuteQueryStore(procgh);
                 helper.Close();
                 foreach (DataRow ghrow in dtghang.Rows)
                 {
                     ListViewItem item = new ListViewItem();
                     item.Text = ghrow[0].ToString();
                     item.SubItems.Add(ghrow[1].ToString());
                     item.SubItems.Add(ghrow[2].ToString());
                     item.SubItems.Add(ghrow[4].ToString());
                     item.SubItems.Add(ghrow[3].ToString());
                     listView1.Items.Add(item);
                 }
                 
             }catch (SqlException ex)
                {
                    MessageBox.Show("Lỗi " + ex.Message);
                }

        }

        private void btntimkiem_Click(object sender, EventArgs e)
        {
            try
            {

                Datahelper helper = new Datahelper();
                helper.Open();
                String username = cmtenkh.Text;
                SqlCommand comman = new SqlCommand();
                comman.CommandText = "sp_TimKiemGHTheoMa";
                comman.CommandType = CommandType.StoredProcedure;
                SqlParameter bien1 = comman.Parameters.Add("@username", SqlDbType.Char,30);
                bien1.Direction = ParameterDirection.Input;
                bien1.Value = username;
                comman.Connection = helper.Connection;
                //lấy ds trả về gán vào listview
                DataTable dttimkiem = new DataTable();
                SqlDataReader reader = comman.ExecuteReader(); 
                listView1.Items.Clear();
                while (reader.Read())
                {
                    ListViewItem item = new ListViewItem();

                    item.Text = reader[0].ToString();
                    item.SubItems.Add(reader[1].ToString());
                    item.SubItems.Add(reader[2].ToString());
                    item.SubItems.Add(reader[3].ToString());
                    item.SubItems.Add(reader[4].ToString());
                    listView1.Items.Add(item);
                }
            }
            catch (System.Exception ex)
            {
                MessageBox.Show("Lỗi  " + ex.Message);
            }
        }

        private void btntatca_Click(object sender, EventArgs e)
        {
             //Load lên listview
            try
            {
                Datahelper helper = new Datahelper();
                helper.Open();
                String username = cmtenkh.Text;
                SqlCommand comman = new SqlCommand();
                comman.CommandText = "sp_LayDSGioHang";
                comman.CommandType = CommandType.StoredProcedure;
                
                comman.Connection = helper.Connection;
                //lấy ds trả về gán vào listview
                DataTable dttimkiem = new DataTable();
                SqlDataReader reader = comman.ExecuteReader(); 
                listView1.Items.Clear();
                while (reader.Read())
                {
                    ListViewItem item = new ListViewItem();

                    item.Text = reader[0].ToString();
                    item.SubItems.Add(reader[1].ToString());
                    item.SubItems.Add(reader[2].ToString());
                    item.SubItems.Add(reader[3].ToString());
                    item.SubItems.Add(reader[4].ToString());
                    listView1.Items.Add(item);
                }
                 
             }catch (SqlException ex)
                {
                    MessageBox.Show("Lỗi " + ex.Message);
                }

        }

        private void btnxemgh_Click(object sender, EventArgs e)
        {
            try
            {
                    gioHangDTO gh = new gioHangDTO();
                    if(listView1.SelectedIndices.Count>0)
                    {
                        gh.MaGioHang = listView1.Items[listView1.SelectedIndices[0]].Text;
                        gh.TenUser = listView1.Items[listView1.SelectedIndices[0]].SubItems[1].Text;
                        gh.NgayDatHang =DateTime.Parse(listView1.Items[listView1.SelectedIndices[0]].SubItems[2].Text);
                        gh.MaTinhTrangGioHang = int.Parse(listView1.Items[listView1.SelectedIndices[0]].SubItems[3].Text);
                        gh.TongTien = float.Parse(listView1.Items[listView1.SelectedIndices[0]].SubItems[4].Text);
                        XemGioHang frm = new XemGioHang();
                        frm.Gh = gh;
                        frm.ShowDialog();
                    }
            }
            catch (System.Exception ex)
            {
                MessageBox.Show("Lỗi " + ex.Message);
            }
        }

        private void rdbtnreaduncommitted_CheckedChanged(object sender, EventArgs e)
        {
           
        }
    }
}